﻿using blackhouse.BlackApart.Business.Discounts;
using blackhouse.BlackApart.Business.Prices;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Data.Caching;
using blackhouse.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;

namespace blackhouse.Aparts.Web.Discounts {
    public class LengthOfStayDiscount : IDiscount {

        #region Fields

        private NemoCache cache = new NemoCache(TimeSpan.FromMinutes(2), ConfigurationManager.AppSettings["MSSQLConnectionString"]);
        private AdoDataHelper dataHelper = new MsSqlAdoDataHelper(ConfigurationManager.AppSettings["MSSQLConnectionString"]);
        private LengthOfStayData[] data;

        #endregion

        #region Methods

        public void Apply(Variant variant, DateTime arrivalDate, DateTime departureDate, int peoples) {
            double lStay = (departureDate.Date - arrivalDate.Date).TotalDays;
            var data = this.GetData(variant.GetGroups()[0].Id);
            data = from d in data where d.LenghtOfStay < lStay orderby d.LenghtOfStay descending select d;
            LengthOfStayData rData = data.FirstOrDefault();
            if (rData.ApartmentTypeId > 0) {
                IPriceRabat longStayRabat = blackhouse.BlackApart.Business.Factory.Instance().GetPriceRabat("longstay");
                if (longStayRabat != null) {
                    longStayRabat.OverrideRabatValue(rData.Rabat);
                    variant.Price.ApplyPriceRabat(longStayRabat);
                }
            }
        }

        public void SetXmlConfiguration(System.Xml.XmlElement configurationNode) {

        }

        private IEnumerable<LengthOfStayData> GetData(int apartmentTypeId) {

            if (data == null)
                this.data = this.GetDbData();
            return (from d in this.data where d.ApartmentTypeId == apartmentTypeId select d);
        }

        protected virtual LengthOfStayData[] GetDbData() {
            cache.Reset();
            cache.SetKey("LenghtOfStay");
            cache.SetDependencyTables("T207");
            LengthOfStayData[] data = cache.GetData<LengthOfStayData[]>();
            if (data == null) {
                List<LengthOfStayData> lData = new List<LengthOfStayData>();
                this.dataHelper.Reset();
                this.dataHelper.Query = "SELECT * FROM T207 WHERE del=0";
                IDataReader reader = this.dataHelper.Execute();
                while (reader.Read()) {
                    LengthOfStayData d = new LengthOfStayData();
                    d.ApartmentTypeId = Convert.ToInt32(reader["typ_apartamentu"]);
                    d.LenghtOfStay = Convert.ToInt32(reader["ilosc_dni"]);
                    d.Rabat = Convert.ToSingle(reader["rabat"]);
                    lData.Add(d);
                }
                reader.Close();
                data = lData.ToArray();
                cache.SetCaching(data);
            }
            return data;
        }

        #endregion

        protected struct LengthOfStayData {
            public int ApartmentTypeId;
            public int LenghtOfStay;
            public float Rabat;
        }

    }
}